//
//  MovieViewController.m
//  chronik01
//
//  Created by conrad on 13.03.11.
//  Copyright 2011 roboboogie.us. All rights reserved.
//

#import "MovieViewController.h"

@implementation MovieViewController

@synthesize theMovie;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */

- (id)initWithMovieName:(NSString*)name type:(NSString*)movieType rect:(CGRect)rect 
{
	if ((self = [super init]) != nil)
	{  
		NSBundle *bundle = [NSBundle mainBundle];
		NSString *moviePath = [bundle pathForResource:name ofType:movieType];
		if (moviePath)
		{
			NSURL *moviePathURL = [NSURL fileURLWithPath:moviePath];
			[self loadMovieAtURL:moviePathURL rect:rect];
			
			
			// Notification
			
			[[NSNotificationCenter defaultCenter] 
			 addObserver:self 
			 selector:@selector(stopAndDestroy)
			 name:MPMoviePlayerPlaybackDidFinishNotification
			 object:nil];
			
		}  
	}
	return self;
}

- (void)loadMovieAtURL:(NSURL*)theURL rect:(CGRect)rect {
	//CGSize size = [[CCDirector sharedDirector] winSize];
	
	self.theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
	
	theMovie.scalingMode = MPMovieScalingModeAspectFill;
	theMovie.controlStyle = MPMovieControlStyleFullscreen;
	[theMovie setFullscreen:TRUE animated:TRUE];
	//theMovie.movieControlMode = MPMovieControlModeHidden;
	
	//theMovie.view.frame = CGRectMake(0, 0, size.width, size.height); 
	theMovie.view.frame = rect;
	theMovie.view.backgroundColor = [UIColor blackColor];
	
	// Transform
	//theMovie.view.transform = CGAffineTransformMakeRotation(-270.0f * (M_PI/180.0f));
	//theMovie.view.center = [[CCDirector sharedDirector] openGLView].center;
	
	[self.view addSubview:theMovie.view];
	
	

}

- (void)play
{
	[theMovie play];
}

- (void)stopAndDestroy {
	
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
	[theMovie pause];
	theMovie.initialPlaybackTime = -1.0;
	[theMovie stop];
	[theMovie.view removeFromSuperview];
	[theMovie release];
	[self.navigationController popViewControllerAnimated:NO];
	
}

-(void) homeButtonViewWasTapped:(HomeButtonView *)hbv {
	[self stopAndDestroy];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
